package il.ac.braude.sona.control;

import il.ac.braude.sona.constants.Mssg;
import il.ac.braude.sona.constants.P;
import il.ac.braude.sona.constants.Resources;
import il.ac.braude.sona.entities.FileTransfer;
import il.ac.braude.sona.gui.forms.FormFrame;
import il.ac.braude.sona.gui.forms.FormListener;
import il.ac.braude.sona.gui.forms.myforms.CreateChangeRequestForm;
import il.ac.braude.sona.ocsf.client.Client;
import il.ac.braude.sona.ocsf.client.ServerResponseListener;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JOptionPane;


/**
 * This class contains create and view change request methods. 
 * @author Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 */
public class CreateChangeRequestController implements  FormListener,ServerResponseListener {

	private CreateChangeRequestForm reqForm;
	private ArrayList<FileTransfer> fileTransfers;
	/**
	 * 
	 * Method to create change request.
	 * 
	 */
	public void execCreateChangeRequest() {

		if (reqForm==null) {
			reqForm  = new CreateChangeRequestForm();
			reqForm.addFormListener(CreateChangeRequestController.this);
			reqForm.showFrame();
		}
		else reqForm.toFront();
	}
	
	//Callbacks//
	
	
	/**
	 * 
	 * Method to handle form cancellation. Cancels only request forms.
	 * @param formFrame - frame to cancel
	 */
	@Override
	public void onFormCanceled(FormFrame formFrame) {

		if (formFrame==reqForm) {
			reqForm.dispose();
			reqForm=null;
		}
	}
	
	
	/**
	 * Method that submits data to database.
	 * 
	 * @param formFrame The form GUI
	 * @param values Values to submit
	 * 
	 */
	@Override
	public void onFormSubmited(FormFrame formFrame, ArrayList<Object> values) {
		reqForm.showMessege(Resources.Strings.PreparingFiles);
		prepareFiles(values);
		reqForm.showMessege(Resources.Strings.Connecting);
		try {
			Mssg msg = new Mssg(P.create,values);
			Client.getInstance().sendToServer(this, msg);
		} catch (Exception e) {
			e.printStackTrace();
			reqForm.showMessege("Something went wrong with the database.");
			reqForm.dispose();
			reqForm=null;
		}
		
	}
	/**
	 * Method that prepares files for upload to server.
	 * 
	 * @param values filenames to upload.
	 */
	
	@SuppressWarnings("unchecked")
	private void prepareFiles(ArrayList<Object> values) {
		ArrayList<File> files = (ArrayList<File>) values.get(values.size()-1);
		fileTransfers = new ArrayList<FileTransfer>();
		for (File f : files )
			try {
				fileTransfers.add(new FileTransfer(f));
			} catch (IOException e) {
			}
		values.remove(files);
	}
	
/**
 * Method that sends message to server as it responds.
 * 
 * @param mssg the message to send.
 * 
 */
	@Override
	public void onServerResponse(Mssg mssg) {
		if (mssg.is(P.error)) {
			reqForm.showMessege((String) mssg.getMessege());
			return;
		}
		final long id = (Long) mssg.getMessege();
		for (FileTransfer f : fileTransfers) f.setChangeRequestId(id);
		reqForm.showMessege(Resources.Strings.UploadingFiles);
		try {
			Client.getInstance().sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					reqForm.dispose();
					reqForm=null;
					if (mssg.is(P.error)) {
						reqForm.showMessege(Resources.Strings.UploadingFilesError);
						return;
					}
					JOptionPane.showMessageDialog(reqForm,String.format(Resources.Strings.RequestApproveID, ""+id));
				}
			}, new Mssg(P.files,fileTransfers));
		} catch (IOException e) {
			reqForm.showMessege(Resources.Strings.UploadingFilesError);
		}

	}

}
